using System.Reflection;
using System.Text;
using SuperSocket.Command;
using SuperSocket.ProtoBase;
using SuperSocket.Server.Abstractions.Session;

namespace Example.Command.Handler;

/// <summary>
/// 减法运算
/// </summary>
[Command(Key = "1002",Name = "sub")]
public class Sub : IAsyncCommand<StringPackageInfo> {
    public ValueTask ExecuteAsync (IAppSession session, StringPackageInfo package, CancellationToken cancelToken) 
    {
        var cmd = $"{this.GetType().GetCustomAttribute<CommandAttribute>()?.Name}";
        
        //Aggregate是一个聚合函数，用于对集合中的元素进行累积操作，返回最终的累积结果
        var result = package.Parameters.Select(int.Parse).Aggregate((x, y) => x - y);
        return session.SendAsync(Encoding.UTF8.GetBytes($"=> [{cmd}]命令被执行,结果是: {result} \r\n\r\n"), cancelToken);
    }
}